﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QuanLyPhongVePhim.Classes;

namespace QuanLyPhongVePhim.Forms
{
    public partial class frmDanhSachNhanVien : Form
    {

        #region events 
        public frmDanhSachNhanVien()
        {
            InitializeComponent();
            btnDeleteEmployee.Text = Properties.Resources.EMP_DELETE_BUTTON_TEXT;
            btnUpdateEmployee.Text = Properties.Resources.EMP_UPDATE_BUTTON_TEXT;
            btnAddEmployee.Text = Properties.Resources.EMP_ADD_BUTTON_TEXT;
        }
        private void frmDanhSachNhanVien_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        private void frmDanhSachNhanVien_FormClosing(object sender, FormClosingEventArgs e)
        {
            //e.Cancel = true;
        }
        private void btnAddEmployee_Click(object sender, EventArgs e)
        {
            HienThiThemNhanVienForm();
        }

       private void btnUpdateEmployee_Click(object sender, EventArgs e)
        {
            var cellindex = this.dtgDanhSachNhanVien.SelectedCells[0].RowIndex;
            var cellcollection = dtgDanhSachNhanVien.Rows[cellindex].Cells[0];
            int employeeId;
            if (cellcollection.Value == null) return;
            if (!int.TryParse(cellcollection.Value.ToString(), out employeeId))
            {
                MessageBox.Show(String.Format(Properties.Resources.COMMON_SELECT_ID, Properties.Resources.EMPLOYEE_TITLE));
            }
            else
            {
                ShowUpdateEmployeeForm(employeeId);
            }
            
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            var cellindex = this.dtgDanhSachNhanVien.SelectedCells[0].RowIndex;
            var cellcollection = dtgDanhSachNhanVien.Rows[cellindex].Cells[0];
            int employeeId;
            if (cellcollection.Value == null) return;
            if (!int.TryParse(cellcollection.Value.ToString(), out employeeId))
            {
                MessageBox.Show(String.Format(Properties.Resources.COMMON_SELECT_ID, Properties.Resources.EMPLOYEE_TITLE));
            }
            else
            {
                if(employeeId>0)
                {
                    DialogResult boxResult = MessageBox.Show( string.Format(Properties.Resources.DELETE_CONFIRM_MES, employeeId), Properties.Resources.COMMON_CAPTION_CONFIRM, MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    if (boxResult == DialogResult.Yes)
                    {
                        NhanVien nv = new NhanVien();
                        nv.MaNhanVien = employeeId;
                        bool del = nv.DeleteEmployee();
                        if (del)
                        {
                            MessageBox.Show(Properties.Resources.COMMON_DEL_SUCCESS);
                            LoadData();
                        }
                        else
                        {
                            MessageBox.Show(Properties.Resources.COMMON_DEL_UNSUCCESS);
                        }
                    }
                }
                
            }
        }

       
        #endregion

        #region private functions

        private void LoadData()
        {
            NhanVien nv = new NhanVien();
            DataTable dsnv = new DataTable();

            dsnv = nv.GetEmployeeList();
            dsnv = FormatDataTable(dsnv);
            this.dtgDanhSachNhanVien.DataSource = dsnv;
        }

        private void HienThiThemNhanVienForm()
        {
            //frmNhanVien frmNhanVien = new frmNhanVien();
            //frmNhanVien.ShowDialog();
            using (frmNhanVien frm = new frmNhanVien())
            {
                frm.EmployeeID = 0;
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    // update datagrid
                    LoadData();
                }
            }
        }


        private void ShowUpdateEmployeeForm( int employeeId)
        {
            using (frmNhanVien fr = new frmNhanVien())
            {
                fr.EmployeeID = employeeId;
                if (fr.ShowDialog() == DialogResult.OK)
                {
                    LoadData();
                }
            }
        }

        private DataTable FormatDataTable(DataTable dt)
        {
            DataTable dtFormat = new DataTable();
            //ColumnHeader header = new ColumnHeader();
            //header.

            dtFormat.Columns.Add("ID");
            dtFormat.Columns.Add("Họ và Tên");
            dtFormat.Columns.Add("Địa Chỉ");
            dtFormat.Columns.Add("Điện Thoại");
            dtFormat.Columns.Add("Giới Tính");
            dtFormat.Columns.Add("Ngày Vào Làm");
            
            foreach (DataRow r in dt.Rows)
            {
                dtFormat.Rows.Add(r["MaNhanVien"].ToString(), string.Format("{0} {1}", r["Ho"], r["Ten"]), r["DiaChi"], r["DienThoai"].ToString(), r["GioiTinh"], String.Format("{0:yyyy/mm/dd}", r["NgayVaoLam"]));
            }
            return dtFormat;

        }

        #endregion

        
    }
}

